<?php

class ProfileController extends MainController implements ControllerInterface {

	/**
	 * Strona glowna
	 *
	 */
	public function IndexAction($param) {
		$this->AddDescription("");
		$this->AddTitle("");
	}

	public function ProfileViewAction($param) {

		$this->smarty->assign('isFriend', 0);

		if (isset($param['idUser'])) {
			try {
				$dalObj = UserDAL::GetDalDataObj();
				//$dalObj->setId($param['idUser']);
				$dalObj->addCondition('user.id_user', $param['idUser']);
				$dalObj->addCondition('status', '1');
				$dalObj->addJoin('UserExtendedProfile', ' JOIN user_extended_profile on (user.id_user = user_extended_profile.id_user)');
				$dalObj->addJoin('UserProfilePermission', ' LEFT JOIN user_profile_permission on (user.id_user = user_profile_permission.id_user)');
				
				$user = UserDAL::GetResult($dalObj);
				
				if (isset($user[0])) {
					$user = $user[0];

					if(is_null($user->userProfilePermission))
							$user->userProfilePermission = new UserProfilePermission();

				} else {
					throw new UserException('no user');
				}
                                
				if($user->getActive()!='1') {
					throw new UserException('no user');
				}


				$this->AddDescription($user->getFirstName() . ' ' . $user->getLastName());
				$this->AddTitle($user->getFirstName() . ' ' . $user->getLastName());
				$this->AddBreadCrumb($user->getFirstName() . ' ' . $user->getLastName());

				$dalObj = CommentsDAL::GetDalDataObj();
				$dalObj->addCondition('id_user', $param['idUser']);
				$dalObj->addCondition('type', Comments::PRODUCT, '!=');
				$dalObj->setCount('id_comments');

				$user->setCommentsCount(CommentsDAL::GetResult($dalObj));

				$dalObj = CommentsDAL::GetDalDataObj();
				$dalObj->addCondition('id_user', $param['idUser']);
				$dalObj->addCondition('type', Comments::PRODUCT);
				$dalObj->setCount('id_comments');

				$user->setProductCommentsCount(CommentsDAL::GetResult($dalObj));

				$dalObj = UserConnectionsDAL::GetDalDataObj();
				$dalObj->addCondition('id_user', $param['idUser']);
				$dalObj->addCondition('active', '1');
				$dalObj->setCount('id_user_connections');

				$user->setConnectionsCount(UserConnectionsDAL::GetResult($dalObj));

				$this->smarty->assign('user', $user);

				$dalObj = UserGalleryDAL::GetDalDataObj();
				$dalObj->addCondition('active', '1');
				$dalObj->addCondition('id_user', $param['idUser']);
				$result = UserGalleryDAL::GetResult($dalObj);

				$this->smarty->assign('userGalleries', $result);

				$dalObj = UserVideoDAL::GetDalDataObj();
				$dalObj->addCondition('active', '1');
				$dalObj->addCondition('id_user', $param['idUser']);
				$result = UserVideoDAL::GetResult($dalObj);

				$this->smarty->assign('userVideos', $result);

				$dalObj = UserDAL::GetDalDataObj();
				$dalObj->addJoin('UserConnections', ' JOIN user_connections ON (user.id_user=user_connections.id_user_target) ');
				$dalObj->addCondition('user.active', '1');
				$dalObj->addCondition('user_connections.id_user', $user->getId());

				$connections = UserDAL::GetResult($dalObj);

				$this->smarty->Assign('connections', $connections);

				$this->smarty->assign('isFriend', 0);

				if (Registry::Get('_userLoggedIn')) {
					$dalData = UserConnectionsDAL::GetDalDataObj();
					$dalData->addCondition('id_user', $this->user->getId());
					$dalData->addCondition('id_user_target', $param['idUser']);
					$dalData->setCount('id_user_connections');
					$isFriend = UserConnectionsDAL::GetResult($dalData);
					$this->smarty->assign('isFriend', $isFriend);
				}

				$userEq = new UserEquipment();
				$eq = $userEq->GetUserEq($param['idUser']);
				$raquet = '';
				foreach ($eq as $item) {
					//$item->category->getParentCategoryObj();
					if($item->category->getParentCategoryObj()->getId()=='1') {
						$raquet = $item;
					}
				}

				$this->smarty->assign('equipment', $eq);
				$this->smarty->assign('raquet', $raquet);

				$usersWallDAL = UsersWallDAL::GetDalDataObj();
				$usersWallDAL->addCondition('users_wall.id_parent', 0);
				$usersWallDAL->addCondition('users_wall.id_user', $param['idUser'], '=');
				//tutaj na dole warunek do znajomych
				//$usersWallDAl->addCondition('id_user','(SELECT )',' IN ',' OR ');
				//$usersWallDAL->addCondition('users_wall.global_users', 1, '=', 'OR');

				$usersWallDAL->setSortBy('date DESC');
				$usersWallDAL->setLimit('4');
				$usersWallDAL->addSelectFields('commentCount', '(SELECT COUNT(a.id_users_wall) FROM users_wall a WHERE a.id_parent = users_wall.id_users_wall)');

				$usersWallDAL->addJoin('User', ' LEFT JOIN user ON(users_wall.id_user = user.id_user)');

				$list = UsersWallDAL::GetResult($usersWallDAL);
				
				$this->smarty->assign('wall', $list);
			} catch (Exception $e) {
//				var_dump($e->getMessage());
//				die();
                                $this->SetNoRender();
				$this->AddDescription('');
				$this->AddTitle('');
				$this->AddRedirectInfo(Utils::Translate('no user found', Registry::Get('_user')), 'error', Router::GenerateUrl());
			}
		} else {
                        $this->SetNoRender();
                        $this->SetAjaxRender();
			$this->AddDescription('');
			$this->AddTitle('');
			$this->AddRedirectInfo(Utils::Translate('no user found', Registry::Get('_user')), 'error', Router::GenerateUrl());
		}
	}
	
	public function PointsAction($param) {
		try {
			$this->AddDescription('');
			$this->AddTitle('');
			
			
			$dalObj = UserDAL::GetDalDataObj();
			//$dalObj->setId($param['idUser']);
			$dalObj->addCondition('user.id_user', $param['idUser']);
			$dalObj->addCondition('status', '1');
			$dalObj->addJoin('UserExtendedProfile', ' JOIN user_extended_profile on (user.id_user = user_extended_profile.id_user)');
			$dalObj->addJoin('UserProfilePermission', ' LEFT JOIN user_profile_permission on (user.id_user = user_profile_permission.id_user)');

			$user = UserDAL::GetResult($dalObj);

			if (isset($user[0])) {
				$user = $user[0];

				if (is_null($user->userProfilePermission))
					$user->userProfilePermission = new UserProfilePermission();
			} else {
				throw new UserException('no user');
			}

			if ($user->getActive() != '1') {
				throw new UserException('no user');
			}
			
			
			$dalObj = UserActivityDAL::GetDalDataObj();
			$dalObj->addJoin('DictActions', ' LEFT JOIN dict_actions on (dict_actions.id_dict_actions = user_activity.id_dict_actions)');
			$dalObj->addCondition('user_activity.id_user', $param['idUser']);
			$activityPoints = UserActivityDAL::GetResult($dalObj);
			
			$this->smarty->assign('activityPoints', $activityPoints);
			$this->smarty->assign('user', $user);
		} catch (Exception $exc) {
			echo $exc->getTraceAsString();
		}
	}

	/**
	 * Czesc wspolna wszystkich podstron
	 *
	 */
	public function PreDispatch($param) {
		$this->addScript('autoresize.jquery.min.js', 'file', 'top');
		$this->Run($param);
	}

	public function PostDispatch($param) {

		// nagłówek strony
		$this->RunShared('Header', $param);


		// footer
		$this->RunShared('Footer', $param);
	}

}

?>